const BaseService = require("./base");

class Service extends BaseService {
	constructor(...args) {
		super(...args);
		this.table = "perm";
	}

	async getPermTree() {
		const menus = await this.app.mysql.query(
			`SELECT id, name, pid FROM menu`
		);
		const permTree = [];
		const map = {};
		menus.forEach((item) => {
			map[item.id] = item;
			if (item.pid === 0) {
				item.children = [];
				permTree.push(item);
			} else {
				map[item.pid].children.push(item);
				item.perms = [];
			}
		});
		const perms = await this.app.mysql.query(
			`SELECT id, menu_id, name, nickname FROM perm`
		);
		perms.forEach((perm) => {
			if (map[perm.menu_id]) {
				map[perm.menu_id].perms.push(perm);
			}
		});

		return permTree;
	}
}

module.exports = Service;
